package com.yunfeng.supercall.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONException;
import org.json.JSONObject;

import com.lin.fragments.RecentCallFragment;
import com.lin.fragments.RecentCallFragment.RunnableSearchList;
import com.lin.utils.Parameters;
import com.lin.utils.Utils;
import com.yunfeng.supercall.adapter.ContactsDialPadAdapter;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.SuperFriendsBean;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

public class HandleRecentCallDataService extends IntentService {
	public static boolean isHandleRecentCallDataServiceRunning = false;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	public HandleRecentCallDataService() {
		// TODO Auto-generated constructor stub
		super("HelloIntentService");
	}

	public HandleRecentCallDataService(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	protected void onHandleIntent(Intent intent) {
		isHandleRecentCallDataServiceRunning = true;

		String data = intent.getStringExtra("data");
		Thread mThread = new Thread(new RunnableSearchList(data));
		mThread.start();


	}

	public class RunnableSearchList implements Runnable {
		String str;

		public RunnableSearchList(String str) {
			super();
			this.str = str;
		}


		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (str.isEmpty()) {
				return ;
			}

			RecentCallFragment.superFriends = new ArrayList<SuperFriendsBean>();
			try {
				RecentCallFragment.mContactsHelper.parseT9InputSearchContacts(str);
			} catch(Exception e) {
				e.printStackTrace();
			}
			try {
				RecentCallFragment.superFriends.addAll(RecentCallFragment.mContactsHelper.getSearchContacts());
				mHandlerSearchList.sendEmptyMessage(0);
			} catch(NullPointerException e) {
				mHandlerSearchList.sendEmptyMessage(1);	
			}
			Log.i("tag123", "superFriends");

		}




	}
	Handler mHandlerSearchList = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(msg.what == 0) {
				Log.i("tag123", "mHandlerSearchList");
				super.handleMessage(msg);
				sendBroadcast(new Intent(Constants.ACTION_UPDATE_BROADCAST));
				stopSelf();
			} else {
				stopSelf();
			}
		}

	};	
}